package com.didispace;

import com.didispace.web.HelloController;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HelloApplication.class)
@WebAppConfiguration
public class HelloApplicationTests3 {

	private MockMvc mvc;

	@Before
	public void setUp() throws Exception {
		mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
	}



	/**
     * hg step0:创建mockMvc
	 * hg step1:创建请求构建器
	 * hg step2：执行请求=》获取结果Action
	 * hg step3：(mark 多次)调用期望方法
	 * hg 		1，创建结果匹配器对象
	 * hg 			1，MockMvcResult s
	 * hg 			2，xxxResult s
	 * hg 			3,调用产生结果匹配器的方法
	 *
	 * @throws Exception
	 */
	@Test
	public void hello1() throws Exception {

		MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/hello");
		builder= builder.accept(MediaType.APPLICATION_JSON);

		ResultActions actions = mvc.perform(builder);



		StatusResultMatchers status = MockMvcResultMatchers.status();
		ResultMatcher ok = status.isOk();
		actions = actions.andExpect(ok);

		ContentResultMatchers content = MockMvcResultMatchers.content();
		ResultMatcher resultMatcher = content.string(CoreMatchers.equalTo("Hello World"));
		actions.andExpect(resultMatcher);
	}

}
